package sapling.service;

import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sapling.dao.*;
import sapling.model.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2017/6/21.
 */
@Service
@Transactional
public class StorageService {

    @Autowired
    private OutboundOrderDao outboundOrderDao;
    @Autowired
    private TreeDao treeDao;
    @Autowired
    private TreeWellDao treeWellDao;
    @Autowired
    private MarkorderDao markorderDao;
    @Autowired
    private TreeWellService treeWellService;
    @Autowired
    private UpdateorderDao updateorderDao;
    @Autowired
    private LandMassDao landMassDao;

    /**
     * 苗木出库
     *
     * @param treeids
     * @param name
     * @return
     */
    public int out(String treeids, String name) {
        int number = 0;
        List<Tree> trees = new ArrayList<>();
        if (!TextUtils.isEmpty(treeids)) {
            OutboundOrder outboundOrder = new OutboundOrder();
            for (String treeid : treeids.split(";")) {
                if(treeid.length()<=0){
                    continue;
                }
                long value = Long.parseLong(treeid);
                Tree tree = treeDao.findOne(value);
                if (Objects.nonNull(tree)) {
                    outboundOrder.setLandMass_id(tree.getLandmassid());
                    tree.setOutWarehouseTime(new Date());
                    tree.setSalesState(2);
                    tree.setLandmassid(0);
                    tree.setTree_well_id(0);
                    if(tree.getTree_well_id()!=0) {
                        TreeWell treeWell = treeWellDao.findbytree(tree.getTree_id());
                        tree.setSerialNumber(treeWell.getLandMass().getAreaLocation().getSerialNumber() + treeWell.getLandMass().getSerialNumber() + "-" + treeWell.getSerialNumber());
                        trees.add(treeDao.save(tree));
                        treeWellService.out(tree.getTree_id());
                    }
                    number++;
                }
            }

            outboundOrder.setName(name);
            outboundOrder.setQuantity(number);
            outboundOrder.setGmt_payment(new Date());
            outboundOrder.setTrees(trees);
            outboundOrderDao.save(outboundOrder);
            for (Tree tree : trees) {
                tree.setOutboundOrder(outboundOrder);
                treeDao.saveAndFlush(tree);
            }
            return number;
        } else {
            return 0;
        }
    }

    public int mark(String treeids, String name, Date days, String remark,int look) {
        int number = 0;
        List<Tree> trees = new ArrayList<>();
        if (!TextUtils.isEmpty(treeids)) {
            for (String treeid : treeids.split(";")) {
                long value = Long.parseLong(treeid);
                Tree tree = treeDao.findOne(value);
                if (Objects.nonNull(tree) && tree.getSalesState() != 3) {
                    tree.setSalesState(3);
                    tree.setRemark(remark);
                    trees.add(treeDao.saveAndFlush(tree));
                    ++number;
                }
            }
            if (Objects.nonNull(trees) && trees.size() > 0) {
                Markorder markorder = new Markorder();
                markorder.setLook(look);
                markorder.setName(name);
                markorder.setSalestate(3);
                markorder.setHaoDate(days);
                markorder.setTreesList(trees);
                markorder.setQuantity(number);
                Markorder saveorder = markorderDao.saveAndFlush(markorder);
                for (Tree tree : trees) {
                    tree.setMarkorder(saveorder);
                    treeDao.saveAndFlush(tree);
                }
            }
            return number;
        }
        return 0;
    }

    public int die(String treeids, String name) {
        int number = 0;
        List<Tree> trees = new ArrayList<>();
        if (!TextUtils.isEmpty(treeids)) {
            for (String treeid : treeids.split(";")) {
                long value = Long.parseLong(treeid);
                Tree tree = treeDao.findOne(value);
                if (Objects.nonNull(tree)) {
                    tree.setSalesState(5);
                    trees.add(treeDao.saveAndFlush(tree));
                    ++number;
                }
            }
            Markorder markorder = new Markorder();
            markorder.setName(name);
            markorder.setSalestate(5);
            markorder.setQuantity(number);
            markorder.setTreesList(trees);
            Markorder saveorder = markorderDao.saveAndFlush(markorder);
            for (Tree tree : trees) {
                tree.setMarkorder(saveorder);
                treeDao.saveAndFlush(tree);
            }
            return number;
        }
        return 0;
    }

    public int updateinfo(String treeids, double chestBreadth,double chestBreadthmax, double crownWidth,double crownWidthmax,
                          double height, double heightmax, int salenumber,int salenumbermax,String remark, String file, String name,double cost) {
        int number = 0;
        if (!TextUtils.isEmpty(treeids)) {
            Updateorder updateorder = new Updateorder();
            updateorder.setChestBreadth(chestBreadth);
            updateorder.setChestBreadthmax(chestBreadthmax);
            updateorder.setCrownWidth(crownWidth);
            updateorder.setCrownWidthmax(crownWidthmax);
            updateorder.setHeight(height);
            updateorder.setHeightmax(heightmax);
            updateorder.setSalenumber(salenumber);
            updateorder.setSalenumbermax(salenumbermax);
            updateorder.setCost(cost);
            updateorder.setImgurl(file);
            updateorder.setName(name);
            updateorder.setRemark(remark);
            updateorder.setQuantity(0);
            updateorderDao.save(updateorder);
            for (String treeid : treeids.split(";")) {
                long value = Long.parseLong(treeid);
                Tree tree = treeDao.findOne(value);
                if (Objects.nonNull(tree)) {
                    tree.setUpdateorder(updateorder);
                    treeDao.save(tree);
                    ++number;
                }
            }
            String ids[]=treeids.split(";");
            if(Objects.nonNull(ids[0])){
                long value = Long.parseLong(ids[0]);
                Tree tree = treeDao.findOne(value);
                LandMass landMass=landMassDao.findOne(tree.getLandmassid());
                if(Objects.nonNull(landMass)){
                    updateorder.setLocation(landMass.getAreaLocation().getSerialNumber()+landMass.getSerialNumber());
                }
            }
            updateorder.setUpdatetime(new Date());
            updateorder.setQuantity(number);
            updateorderDao.save(updateorder);
        }
        return number;
    }

    public void confirmUpdate(Long updateorder_id,double cost) {
        if (Objects.nonNull(updateorder_id)) {
            Updateorder updateorder = updateorderDao.findOne(updateorder_id);
            if (Objects.nonNull(updateorder)) {
                for (Tree tree : updateorder.getTreesList()) {
                    tree.setChestBreadth(updateorder.getChestBreadth());
                    tree.setChestBreadthmax(updateorder.getChestBreadthmax());
                    tree.setCrownWidth(updateorder.getCrownWidth());
                    tree.setCrownWidthmax(updateorder.getCrownWidthmax());
                    tree.setHeight(updateorder.getHeight());
                    tree.setHeightmax(updateorder.getHeightmax());
                    tree.setSalenumber(updateorder.getSalenumber());
                    tree.setSalenumbermax(updateorder.getSalenumbermax());
                    tree.setUsername(updateorder.getName());
                    if(cost>0){
                        tree.setCost(cost);
                    }else {
                        tree.setCost(updateorder.getCost());
                    }
                    if(Objects.nonNull(updateorder.getImgurl())&&updateorder.getImgurl().length()>0){
                        tree.setImgurl(updateorder.getImgurl());
                    }
                    if(Objects.nonNull(updateorder.getRemark())&&updateorder.getRemark().length()>0){
                        tree.setRemark(updateorder.getRemark());
                    }
                    tree.setUpdateorder(null);
                    treeDao.saveAndFlush(tree);
                    treeWellService.record(tree.getTree_id(), tree.getTreeSpecies(), tree.getHeight(), tree.getHeightmax(),tree.getChestBreadth(),tree.getChestBreadthmax(),
                            tree.getCrownWidth(),tree.getCrownWidthmax(), tree.getSalenumber(),tree.getSalenumbermax(),tree.getImgurl(),(cost>0)?cost:tree.getCost());
                }
                updateorderDao.delete(updateorder_id);
            }
        }
    }
}